package com.baizhi.cmfz.realm;

import com.baizhi.cmfz.dao.AdminDao;
import com.baizhi.cmfz.entity.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.tomcat.util.descriptor.web.ContextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AuthenRealm extends AuthenticatingRealm {

    @Autowired
    private AdminDao adminDao;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
 //         1获取token中的数据，
        UsernamePasswordToken token1= (UsernamePasswordToken) token;
        String username=token1.getUsername();
 //        2根据用户名查找数据，
        Admin admin=adminDao.showNameAndPwd(username);

        // 3查询结果不为空封装info
        if(admin!=null){
            //参数以数据库中查询的数据，2密码 ， 3类名
            return new SimpleAuthenticationInfo(admin.getUsername(),admin.getPassword(),this.getName());
        }
        return null;
    }
}
